package com.tsx.wiki.controller;

import com.tsx.wiki.dto.CategoryDTO;
import com.tsx.wiki.dto.CategoryPageDTO;
import com.tsx.wiki.pojo.Category;
import com.tsx.wiki.result.PageResult;
import com.tsx.wiki.result.Result;
import com.tsx.wiki.service.CategoryService;
import com.tsx.wiki.vo.CategoryVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    public Result<PageResult<Category>> page(@Valid CategoryPageDTO categoryPageDTO) {
        PageResult<Category> pageResult = categoryService.page(categoryPageDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result<List<Category>> list(CategoryDTO categoryDTO) {
        List<Category> categories = categoryService.list(categoryDTO);
        return Result.success(categories);
    }

    @GetMapping("/{id}")
    public Result<CategoryVo> queryById(@PathVariable Long id) {
        CategoryVo category = categoryService.selectById(id);
        return Result.success(category);
    }

    @PutMapping()
    public Result<Object> updateById(@Valid @RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @PostMapping()
    public Result<Object> save(@Valid @RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Object> deleteById(@PathVariable Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }
}
